﻿using System;

/*
 * 创建人：
 * 
 * 时间：
 * 
 * 说明：
 * 
 * 
 * 修改说明：
 * 
 * 
 */


namespace ConsoleTest.ChapterFive
{
    /// <summary>
    /// 进程与线程相关测试代码
    /// </summary>
    public class Thread 
    {

        //thread构造函数参数
        //1、 System.Threading.ParameterizedThreadStart  委托，用于指定在thread上执行的方法
        //2、 System.Threading.ThreadStart 委托，同样指定线程开始时要执行的方法
        //3、System.Threading.ParameterizedThreadStart  委托，另外指定该线程的栈的大小
        //4、 System.Threading.ThreadStart  委托，另外指定该线程的栈的大小
        //为什么可以直接传递一个方法呢？
        //这是一个语法上面的技巧，其实际仍然是使用了委托类的构造函数生成一个委托实例
        //可以通过 Reflector工具或者ildasm工具查看


        /// <summary>
        /// 获取当前程序的进程名称
        /// </summary>
        public static void GetNowProcess()
        {
            System.Diagnostics.Process nowProcess = System.Diagnostics.Process.GetCurrentProcess();
            System.Console.WriteLine("now process: "+nowProcess.ProcessName);
            
        }

        /// <summary>
        /// 创建一个进程，等待一段时间后关闭进程
        /// </summary>
        public static void CreateProcess()
        {
            //创建一个子进程，启动笔记本
            //System.Diagnostics.Process process = System.Diagnostics.Process.Start(@"C:\Windows\System32\notepad.exe", "NewTxt");

            //静态Start方法可以使用Windows文件扩展名关联机制
            System.Diagnostics.Process process = System.Diagnostics.Process.Start("Hello.txt");

            //父进程等待1秒
            System.Threading.Thread.Sleep(60000);

            process.Kill();

        }

        /// <summary>
        /// 运行单个进程实例
        /// </summary>
        public static void RunSingletonProcess()
        {
            System.Diagnostics.Process procCurrent = System.Diagnostics.Process.GetCurrentProcess();
            System.Diagnostics.Process[] procs = System.Diagnostics.Process.GetProcesses();

            foreach (System.Diagnostics.Process proc in procs)
            {
                if (procCurrent.Id != proc.Id)
                {
                    if (procCurrent.ProcessName == proc.ProcessName)
                    {
                        System.Console.WriteLine("process: {0} is already running!", proc.ProcessName);
                        break;
                    }
                    else
                    {
                        System.Console.WriteLine("first process of {0}", procCurrent.ProcessName);
                    }
                    
                }
            }
        }

        /// <summary>
        /// Join方法测试
        /// Join方法阻塞主调线程，直到被调用的线程执行结束
        /// </summary>
        public static void ThreadJoin()
        {
            System.Threading.Thread.CurrentThread.Name = "MainThread";

            System.Threading.Thread newThread =new System.Threading.Thread(new System.Threading.ThreadStart(JoinTestFun));
            newThread.Name = "NewThread";

            for (int j = 0; j < 20; j++)
            {
                if (j == 10)
                {
                    newThread.Start();
                    //阻塞调用线程，直到线程终止
                    newThread.Join();
                }
                else
                {
                    System.Console.WriteLine(System.Threading.Thread.CurrentThread.Name + "   j =  " + j);
                }
            }

        }

        /// <summary>
        /// Join方法测试
        /// </summary>
        private static void JoinTestFun()
        {
            for (int i = 0; i < 10; i++)
            {
                System.Console.WriteLine(System.Threading.Thread.CurrentThread.Name + "   i =  " + i);
            }
            System.Console.WriteLine(System.Threading.Thread.CurrentThread.Name + " has finished");
        }

        /// <summary>
        /// Thread Abort方法
        /// </summary>
        public static void ThreadAbort()
        {
            /*
             * 在调用此方法的线程上引发 ThreadAbortException，以开始终止此线程的过程。调用此方法通常会终止线程。 
             */
            try
            {
                System.Threading.Thread.CurrentThread.Abort();
            }
            catch (System.Threading.ThreadAbortException e)
            {
                System.Console.WriteLine("Thread state:" + System.Threading.Thread.CurrentThread.ThreadState.ToString());
                System.Console.WriteLine(e.Message);
            }
            
        }

        /// <summary>
        /// 
        /// </summary>
        public static void ThreadSuspend()
        {
            System.Threading.Thread.CurrentThread.Name = "MainThread";

            System.Threading.Thread newThread = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadSuspendFun));
            newThread.Name = "NewThread";

            newThread.Start();
            for (int j = 0; j < 200; j++)
            {

                if (j == 10)
                {
                    //挂起线程
                    newThread.Suspend();
                }
                else
                {
                    System.Console.WriteLine(System.Threading.Thread.CurrentThread.Name + "   j =  " + j);
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        private static void ThreadSuspendFun()
        {
            for (int i = 0; i < 1000; i++)
            {
                System.Console.WriteLine(System.Threading.Thread.CurrentThread.Name + "   i =  " + i);
            }
            System.Console.WriteLine(System.Threading.Thread.CurrentThread.Name + " has finished");

            System.Threading.Thread.CurrentThread.Suspend();
        }



    }


}